import java.io.File;
import java.net.URL;
import java.security.ProtectionDomain;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.nio.NetworkTrafficSelectChannelConnector;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.webapp.WebAppContext;

public class Runner {

	// public static void main(String[] args) throws Exception {
	// int port = Integer.parseInt(System.getProperty("port", "8088"));
	// Server server = new Server(port);
	// System.out.println("xxxxxxxxxxxxx");
	//
	// ProtectionDomain domain = Runner.class.getProtectionDomain();
	// URL warLocation = domain.getCodeSource().getLocation();
	//
	// WebAppContext webapp = new WebAppContext();
	// webapp.setContextPath("/");
	// webapp.setDescriptor(warLocation.toExternalForm() + "/WEB-INF/web.xml");
	// webapp.setServer(server);
	// webapp.setWar(warLocation.toExternalForm());
	// String tempDir = new File(warLocation.getPath()).getParent() + "/tmp";
	// webapp.setTempDirectory(new File(tempDir));
	//
	// server.setHandler(webapp);
	// server.start();
	// server.join();
	// }
	public static void main(String[] args) throws Exception {
		String jettyVersion = Server.getVersion();
		final int port = Integer.parseInt(System.getProperty("port", "82"));
		ProtectionDomain protectionDomain = Runner.class.getProtectionDomain();
		URL location = protectionDomain.getCodeSource().getLocation();
		String warFile = location.toExternalForm();
		String currentDir = new File(location.getPath()).getParent();
		File workDir = new File(currentDir, "work");

		System.out.println("##########jettyVersion=" + jettyVersion);
		System.out.println("##########port=" + port);
		System.out.println("##########currentDir=" + currentDir);
		System.out.println("##########workDir=" + workDir);
		System.out.println("##########warFile=" + warFile);

		WebAppContext webapp = new WebAppContext();
		webapp.setContextPath("/");
		// webapp.setResourceBase(".");
		webapp.setTempDirectory(workDir);
		// webapp.setClassLoader(Thread.currentThread().getContextClassLoader());
		webapp.setWar(warFile);

		NetworkTrafficSelectChannelConnector connector = new NetworkTrafficSelectChannelConnector();
		connector.setPort(port);
		connector.setMaxIdleTime(60000);
		connector.setRequestHeaderSize(10240);
		connector.setSoLingerTime(-1);
		connector.setThreadPool(new QueuedThreadPool(1024));

		Server server = new Server();
		server.addConnector(connector);
		server.setHandler(webapp);
		server.start();
		server.join();
	}
}
